/**
 * Redis包装.
 */
const models = require('./models');
const Redis = require('redis');

Promise.promisifyAll(Redis.RedisClient.prototype);
Promise.promisifyAll(Redis.Multi.prototype);

let rc = null;

module.exports = {
    async init() {
        if (!rc) {
            const settingsModel = think.model(models.Settings);
            const settings = await settingsModel.getServerSideSettings();
            rc = Redis.createClient(settings.redis.port, settings.redis.host);
            rc.auth(settings.redis.auth);

            rc.on('ready', function () {
                think.logger.info('Redis 连接成功.');
            });

            rc.on("error", function (err) {
                think.logger.error('Redis 连接失败。原因: ' + err.message);
            });
        }
    },

    get client() {
        return rc;
    }
};
